package main

import "fmt"

func main() {
	ratings := []int{1, 0, 2}
	v := candy(ratings)
	fmt.Println(v)
}

// 注意第二次遍历的时候 有两个判断条件
func candy(ratings []int) int {
	n := len(ratings)
	num := make([]int, n)
	for i, r := range ratings {
		if i != 0 && r > ratings[i-1] {
			num[i] = num[i-1] + 1
		} else {
			num[i] = 1
		}
	}
	for i := n - 2; i >= 0; i-- {
		if ratings[i] > ratings[i+1] && num[i] <= num[i+1] {
			num[i] = num[i+1] + 1
		}
	}
	sum := 0
	for _, v := range num {
		sum += v
	}
	return sum
}
